$(function () {

    //首次打开页面时获取默认数据
    energyAnalysis($("#customerinfoid").val(), $("#pointid").val(), $("#endDate").val());

    //选择一个客户
    $("#companyName").dblclick(function () {
        $.baseCustomerApi({
            getBaseValue: function (customer) {
                $("#customerinfoid").val(customer.id);
                $("#companyCode").val(customer.companycode);
                $("#companyName").val(customer.customername);
                getAreas(customer.id);
            }
        });
    });

    //执行查询操作
    $("#searchEnergy").click(function () {
        $("#errormessage").empty();
        energyAnalysis($("#customerinfoid").val(), $("#pointid").val(), $("#endDate").val());
    });
});

function getAreas(customerinfoid) {
    $.ajax({
        contentType: "application/json",
        url: "getAreas.shtml",
        data: {"customerinfoid": customerinfoid},
        async: false,
        dataType: "json",
        success: function (data) {
            console.log(data);
            var points = document.getElementById('pointid');
            points.length = 0;
            if (data.length > 0) {
                $.each(data, function (item,element) {
                    points.options[points.length] = new Option(element.areaname, element.pointid);
                })
            }else{
                points.options[0] = new Option("暂无区域","");
            }

        }
    })
}

function energyAnalysis(customerinfoid, pointid, endDate) {
    var customer = $("#companyName").val();
    var area = $('#pointid :selected').text();
    var param = {
        "customerinfoid": customerinfoid,
        "endDate": endDate,
        "pointid": pointid
    };
    $.ajax({
        contentType: "application/json",
        url: "areaState.shtml",
        data: param,
        async: false,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                var date;
                var series = [];
                var serie = {};
                serie.name = endDate;
                var state = [];
                $.each(data, function (item, element) {
                    date = element.startdate;
                    date = date + 28800000;
                    var ele = [];
                    ele[0] = date;
                    var elestate = element.activestate == 1 ? 1 : null;
                    ele[1] = elestate;
                    state[state.length] = ele;
                });
                serie.data = state;
                series[0] = serie;
                linear_date('container', '区域用电分析', area + " " + endDate, '', 1, '', series
                )
                ;
            } else {
                $("#container").empty();
                $("#errormessage").html("该区域暂无数据")
            }
        }
    });
}
